package com.lswms.modules.lswms.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.common.utils.ConvertUtils;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dao.base.CodeDao;
import com.lswms.modules.lswms.dao.base.ZoneDao;
import com.lswms.modules.lswms.dto.base.ZoneDTO;
import com.lswms.modules.lswms.entity.base.CodeEntity;
import com.lswms.modules.lswms.entity.base.ZoneEntity;
import com.lswms.modules.lswms.service.base.ZoneService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 区 
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-06
 */
@Service
public class ZoneServiceImpl extends CrudServiceImpl<ZoneDao, ZoneEntity, ZoneDTO> implements ZoneService {

	@Autowired
	private ZoneDao zoneDao;
    @Override
    public QueryWrapper<ZoneEntity> getWrapper(Map<String, Object> params){
    	 QueryWrapper<ZoneEntity> wrapper = new QueryWrapper<>();
    	if(null == params) {
    		
    	}else {
	        String id = (String)params.get("id");
	
	       
	        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
    	}
        return wrapper;
    }

	@Override
	public String validate(ZoneDTO dto) {
		String zoneCode =dto.getZonecode() ; 
		
		if( StringUtils.isEmpty(zoneCode)) {
			return  String.format(MEGConstant.IS_NOT_EMPTY, "ZoneCode")  ;
		}
		if( StringUtils.isEmpty(dto.getZonename())) {
			return  String.format(MEGConstant.IS_NOT_EMPTY, "ZoneName")  ;
		}
		
		 QueryWrapper<ZoneEntity> wrapper = new QueryWrapper<>();
		 wrapper.eq("zonecode", zoneCode);
		 Integer count = zoneDao.selectCount(wrapper);
		 if(count>0) {
			 return  String.format(MEGConstant.VALUE_EXISTS, zoneCode )  ;
		 }
		return "";
	}

	@Override
	public List<ZoneDTO> getzonelisthasInventory(Object object) {
		 QueryWrapper<ZoneEntity> wrapper = new QueryWrapper<>();
		 wrapper.exists("select 1 from lswms_location ,lswms_inventory where lswms_location.zonecode = lswms_zone.zonecode"
		 		+ " and lswms_location.location = lswms_inventory.location and lswms_inventory.qty>0 and lswms_inventory.sku ='KX' ");
		 List<ZoneEntity> list = baseDao.selectList(wrapper);
		 
		  List<ZoneDTO> list1 = ConvertUtils.sourceToTarget(list, ZoneDTO.class);
		return list1;
	}

}